package elijah.base.dialog;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.viewbinding.ViewBinding;

import com.lxj.xpopup.core.BasePopupView;

import elijah.base.databinding.BaseViewItemCreateBinding;
import elijah.base.dialog.api.OnActionCallback;
import elijah.base.utils.ClassUtils;
import elijah.base.utils.DialogUtils;

public class BaseItemCreateView<T> extends BaseDialogView<BaseViewItemCreateBinding> {
    private Builder<T> builder;

    private BaseItemCreateView(@NonNull Context context) {
        super(context);
    }


    @Override
    protected void initView() {
        binding.title.setText(builder.getTitle());
        binding.contentContainer.addView(builder.getContentView());
        binding.btAdd.setVisibility(builder.getAddActionClickListener() == null ? GONE : VISIBLE);
        binding.btAdd.setOnClickListener(builder.getAddActionClickListener() == null ? null : v -> {
            if (builder.getAddActionClickListener() != null) {
                builder.getAddActionClickListener().action(BaseItemCreateView.this);
            }
        });

        if (builder.getActionClickCallback() != null) {
            binding.btAction.setVisibility(VISIBLE);
            binding.btAction.setText(builder.getActionName());
            binding.btAction.setOnClickListener(v -> builder.getActionClickCallback().action(BaseItemCreateView.this));
        }
        if (builder.actionCallback != null) {
            builder.contentView.attachCallback(builder.actionCallback, dialog);
        }

        builder.contentView.actionButton(binding.btnCancel, binding.btnSubmit);
        binding.btnSubmit.setOnClickListener(view -> builder.contentView.onSubmit(builder.actionCallback, dialog));
        binding.btnCancel.setOnClickListener(view -> DialogUtils.dismiss(dialog));
        binding.btnSubmit.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                binding.btnSubmit.performClick();
            }
        });

    }


    private BaseItemCreateView<T> show(Builder<T> builder) {
        this.builder = builder;
        setPopupConfig(builder.popupConfig);
        super.show(builder.getViewWidth());
        return this;
    }


    public static class Builder<T> {
        private final Context context;
        private String title;
        private float viewWidth = 0.5f;
        private IContentView<T, ?> contentView;
        private OnActionCallback<T> actionCallback;
        private ActionClickCallback addActionClickListener;
        private String actionName;
        private ActionClickCallback actionClickCallback;

        private PopupConfig popupConfig;

        public Builder<T> setPopupConfig(PopupConfig popupConfig) {
            this.popupConfig = popupConfig;
            return this;
        }

        public Builder(Context context) {
            this.context = context;
        }

        public Builder<T> setActionClickCallback(String actionName, ActionClickCallback onActionClickCallback) {
            this.actionClickCallback = onActionClickCallback;
            this.actionName = actionName;
            return this;
        }

        public ActionClickCallback getActionClickCallback() {
            return actionClickCallback;
        }

        public String getActionName() {
            return actionName;
        }

        public String getTitle() {
            return title;
        }

        public Builder<T> setTitle(String title) {
            this.title = title;
            return this;
        }

        public float getViewWidth() {
            return viewWidth;
        }

        public Builder<T> setViewWidth(float viewWidth) {
            this.viewWidth = viewWidth;
            return this;
        }

        public IContentView<T, ?> getContentView() {
            return contentView;
        }

        public Builder<T> setContentView(IContentView<T, ?> contentView) {
            this.contentView = contentView;
            return this;
        }

        public OnActionCallback<T> getActionCallback() {
            return actionCallback;
        }

        public Builder<T> setActionCallback(OnActionCallback<T> actionCallback) {
            this.actionCallback = actionCallback;
            return this;
        }

        public ActionClickCallback getAddActionClickListener() {
            return addActionClickListener;
        }

        public Builder<T> setAddActionClickListener(ActionClickCallback addActionClickListener) {
            this.addActionClickListener = addActionClickListener;
            return this;
        }

        public BaseItemCreateView<T> show() {
            return new BaseItemCreateView<T>(context).show(this);
        }
    }

    public abstract static class IContentView<T, V extends ViewBinding> extends FrameLayout {
        protected V binding;


        public IContentView(@NonNull Context context) {
            super(context);
            binding = getViewBinding();
        }

        protected V getViewBinding() {
            return (V) ClassUtils.getViewBinding(getClass(), 1, this, true);


        }

        public abstract void onSubmit(OnActionCallback<T> callback, BasePopupView dialog);


        public void actionButton(View cancel, View submit) {

        }

        public void attachCallback(OnActionCallback<T> actionCallback, BasePopupView dialog) {

        }

        public Builder<T> builder() {
            return new BaseItemCreateView.Builder<T>(getContext())
                    .setContentView(this);

        }

    }

    public interface ActionClickCallback {
        void action(BaseItemCreateView<?> baseView);
    }


}
